package fun.chutianshu;

//玩家类和怪物类的父类
public class Character {
    private String name;
    private int hp;
    private int ap;


    public Character(String name, int hp, int ap) {
        this.name = name;
        this.hp = hp;
        this.ap = ap;
    }

    public Character() {}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public int getAp() {
        return ap;
    }

    public void setAp(int ap) {
        this.ap = ap;
    }

    public void printInfo() {
        System.out.println("名字：" + this.name + ",生命：" + this.hp + ",攻击力：" + this.ap);
    }

    //父类的对象可以直接引用子类对象，称为 “is a” 关系
    public void pk(Character enemy){
        while(true){
            //不考虑速度，假设每次都是玩家先出手
            enemy.setHp(enemy.getHp()-this.getAp());
            System.out.println(this.getName()+"攻击："+enemy.getName());
            System.out.println(enemy.getName()+"受到"+this.getAp()+"点伤害，剩余生命值："+enemy.getHp());
            //判断怪物是否活着，如果活着的话，还手
            if(enemy.getHp()>0)
            {
                this.setHp(this.getHp() - enemy.getAp());
                System.out.println(enemy.getName()+"攻击："+this.getName());
                System.out.println(this.getName()+"受到"+enemy.getAp()+"点伤害，剩余生命值："+this.getHp());

                //判断玩家是否还活着
                if(this.getHp()<=0)
                {
                    System.out.println(this.getName()+"挂了，game over！");
                    return;
                }
            }
            else {
                System.out.println(enemy.getName()+"挂了，"+this.getName()+"获取经验和物品");
                return;
            }
        }
    }
}
